package com.logic.modular.game.player.bag.service;

import cn.hutool.core.util.RandomUtil;

import com.comment.exception.ServiceException;
import com.logic.modular.game.player.hero.service.PlayerHeroServiceImpl;
import com.logic.modular.game.player.skill.PlayerSkillServiceImpl;
import com.comment.pojo.domain.logic.core.CoreProp;
import com.logic.modular.gameCore.cultivate.prop.CorePropServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.Set;

/**
 * 背包物品服务---武将或者战法礼包
 */
@Slf4j
@Service

public class PlayerPropType3ServiceImpl {
    @Resource
    private BagItemServiceImpl bagItemService;
    @Resource
    private PlayerHeroServiceImpl playerHeroServiceImpl;
    @Resource
    private PlayerSkillServiceImpl playerSkillServiceImpl;

    @Transactional
    public void useGiftPack(
            Long playerId,
            Long corePropId,
            Long... selectionOptions
    ) {
        try {
            bagItemService.obtainBagItem(playerId, corePropId, new BigDecimal(1).negate());
            CoreProp cache = CorePropServiceImpl.getCache(corePropId);
            Integer useValueType = cache.getUseValueType();
            Map<Long, Integer> useValue = cache.getUseValue();
            obtainGift(playerId, useValueType, useValue, selectionOptions);
        } catch (Exception e) {
            // 删除缓存防止缓存不一致
            log.error("使用礼包失败", e);
            bagItemService.removeCache(playerId);
            throw new ServiceException("使用礼包" + corePropId + "失败");
        }
    }

    private void obtainGift(
            Long playerId,
            Integer useType,
            Map<Long, Integer> useValue,
            Long[] selectionTargetGiftIds
    ) {
        Set<Long> longs = useValue.keySet();
        Long targetGiftId = (Long) RandomUtil.randomEle(longs.toArray());
        Integer num = useValue.get(targetGiftId);
        switch (useType) {
            case 1:// 随机 一项 物品
                bagItemService.obtainBagItem(playerId, targetGiftId, new BigDecimal(num));
                break;
            case 2: // 随机 一项 战法
                for (int i = 0; i < num; i++) {
                    playerSkillServiceImpl.obtainSkill(playerId, targetGiftId);
                }
                break;
            case 3: // 随机 一项 武将
                playerHeroServiceImpl.obtainHero(playerId, targetGiftId, num);
                break;
            case 4:// 自选一个战法
                if (selectionTargetGiftIds.length > 1) {
                    throw new ServiceException("该礼包只能选一项");
                }
                Long selectionSkillGiftId = selectionTargetGiftIds[0];
                Integer skillNum = useValue.get(selectionSkillGiftId);
                for (int i = 0; i < skillNum; i++) {
                    playerSkillServiceImpl.obtainSkill(playerId, selectionSkillGiftId);
                }
                break;
            case 5:// 自选一个武将
                if (selectionTargetGiftIds.length > 1) {
                    throw new ServiceException("该礼包只能选一项");
                }
                Long selectHeroGiftId = selectionTargetGiftIds[0];
                Integer heroNum = useValue.get(selectHeroGiftId);
                playerHeroServiceImpl.obtainHero(playerId, selectHeroGiftId, heroNum);
                break;
            default:
                log.error("useType:{}", useType);
                break;
        }
    }
}
